//
//  NetworkTool.m
//  网易新闻
//
//  Created by HM on 16/9/2.
//  Copyright © 2016年 HM. All rights reserved.
//

#import "NetworkTool.h"

@implementation NetworkTool

+ (instancetype)sharedTool
{
    static NetworkTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        // 配置baseURL
        NSURL *baseURL = [NSURL URLWithString:@"http://c.m.163.com/nc/"];
        
        instance = [[self alloc] initWithBaseURL:baseURL];
        
        // 增加AFN支持的文件类型
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    });
    
    instance.requestSerializer = [AFJSONRequestSerializer serializer];
    
    return instance;
}

- (void)GETWithURLStr:(NSString *)URLStr parameters:(id)parameters success:(void (^)(id))successBlock failed:(void (^)(NSError *error))failedBlock
{
    [self GET:URLStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (successBlock != nil) {
            
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failedBlock != nil) {
            
            failedBlock(error);
        }
    }];
}


- (void)POSTWithURLStr:(NSString *)URLStr parameters:(id)parameters success:(void (^)(id))successBlock failed:(void (^)(NSError *error))failedBlock
{
    [self POST:URLStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (successBlock != nil) {
            
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failedBlock != nil) {
            
            failedBlock(error);
        }
    }];
}

@end
